package org.jash.shop2112.entity

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import org.jash.shop2112.BR
import org.jash.shop2112.utils.bus

data class CartItem(
    val goods_default_icon: String,
    val goods_default_price: Float,
    val goods_desc: String,
    val goods_id: Int,
    val id: Int,
    val order_id: Int,
    val user_id: Int
) : BaseObservable() {
    @Bindable
    var checked:Boolean = false
        set(value) {
            field = value
            bus.postValue("refreshAll" to "")
            notifyPropertyChanged(BR.checked)
        }
    @Bindable
    var count: Int = 0
        set(value) {
            if (value <= 0) {
                return
            }
            field = value
            notifyPropertyChanged(BR.count)
            if (checked) {
                bus.postValue("refreshAll" to "")
            }
        }
    fun plusCount(i:Int) {
        count += i
    }
    fun delete() {
        bus.postValue("删除购物车" to this)
    }
    @get:Bindable("count")
    val subTotal:Float
        get() = goods_default_price * count
    // 第二个半价
    @get:Bindable("count")
    val coupon:Float
        get()=  subTotal - (count / 2) * goods_default_price * 0.5f

}